﻿using System;
using System.Collections.Generic;
using System.Web;
using WebMatrix.Data;
/// <summary>
/// Summary description for ClassName
/// </summary>
public class DBManager
{
    private static Database db;
    public DBManager()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    
    private static bool Connect(){
        
        try{
            db = Database.Open("CCMS");
            return true;
        }catch(Exception e){
            throw e;
        }
    }
    
    private static void Close(){
        try{
            db.Close();
            db.Dispose();
        }catch(Exception e){
            throw e;
        }finally{
            db.Close();
            db.Dispose();
        }
    }
    
    public static IList<dynamic> Query(string sql,object[] param){
        if(Connect()){
            IEnumerable<dynamic> query;
            if(param!=null)
              query = db.Query(sql,param);
            else
              query = db.Query(sql);
            return new List<dynamic>(query);
        }
        Close();
        return new List<dynamic>();
    }
    
    public static int Execute(string sql,object[] param){
        if(Connect()){
                var result = db.Execute(sql,param);
                return result;
            }//end of elseif
          return 2;
        Close();
        }
      
    
    
}
